<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">
          添加分类</el-button>
      </div>
      <!-- 列表 -->
      <el-table :data="cateList" border stripe style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="140">
        </el-table-column>

        <!-- 分类名称 -->
        <el-table-column prop="cate_name" label="分类名称" width="180">
        </el-table-column>

        <!-- 分类别名 -->
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <!-- 操作 --> 
        <el-table-column label="操作">
             <template v-slot='{row}'> 
               <!-- <p>{{row.id}}</p> -->
                 <el-button type="primary" size="min"  @click='showUpda(row.id)'>修改</el-button>
                  <el-button type="warning" size="min" @click="delArtCate(row.id)">删除</el-button>
             </template>
        </el-table-column>

      </el-table>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog title="添加文章分类" 
    :visible.sync="addVisible" 
    width="35%"
    @closed='$refs.addRef.resetFields()'
    >
      <!-- 分类表单 -->
<el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
  </el-form-item>
</el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>
   <!-- 添加分类的对话框 结束 -->

     <!-- 修改文章分类的对话框 -->
    <el-dialog title="修改文章分类" 
    :visible.sync="updateVisible" 
    width="35%"
     @closed='$refs.updateRef.resetFields()'
    >
  <!-- 修改分类表单 -->
<el-form :model="updateForm" :rules="addRules" ref="updateRef" label-width="80px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="updateForm.cate_name" minlength="1" maxlength="10"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="updateForm.cate_alias" minlength="1" maxlength="15"></el-input>
  </el-form-item>
</el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCate ">确 定</el-button>
      </span>
    </el-dialog>
   <!-- 修改文章分类的对话框 结束-->  
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      //控制添加显示隐藏
      addVisible: false,
      //修改显示隐藏
      updateVisible:false,

      //添加数据对象
       addForm: {
      cate_name: '',
      cate_alias: ''
      },
      //修改数据对象
       updateForm: {
      cate_name: '',
      cate_alias: ''
      },

      // 表单验证规则
    addRules: {
      cate_name: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
        { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
      ],
      cate_alias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
      ]
    }
    }
  },
  //调用函数
  created() {
    this.initList()
  },
  methods: { 
       //根据id 删除数据
       delArtCate(id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
            //请求数据
          const {data:res} =await this.$http.delete('/my/cate/del',{
             params:{id}
          })
          console.log(res)
              if(res.code!=0) return this.$message.error(res.message)
            this.$message.success(res.message)
            // 重新 渲染数据
            this.initList()
        }).catch(() => {
            //取消删除       
        });
      },
      
     //修改文章分类数据
      updateCate(){
         //表单校验
        this.$refs.updateRef.validate(async(val)=>{
          // console.log(val)
           if(!val)  return
          //请求数据 
          const {data:res} = await this.$http.put('/my/cate/info',this.updateForm)
          if(res.code!=0) return this.$message.error(res.message)
            this.$message.success(res.message)
            //关闭对话框
            this.updateVisible = false
            // 重新 渲染数据
            this.initList()
        })  
      },

// 展示编辑的对话框
async showUpda(id) {
  // 1. 获取分类的基本信息
  const { data: res } = await this.$http.get('/my/cate/info', {
    params: { id }
  })
     console.log(res);
  if (res.code === 0) {
    // 表单赋值
    this.updateForm = res.data
    //显示对话框
    this.updateVisible = true
  }
},
    //添加文章分类
    addArtCate(){
      this.$refs.addRef.validate(
         async (valid)=>{
        if(!valid)  return 
        const {data:res} = await this.$http.post('/my/cate/add',this.addForm)
        if(res.code!=0) return this.$message.error(res.message)
        this.$message.success(res.message)
        //关闭对话框
       this.addVisible = false
       //请求列表数据
         this.initList()
      })
    },
     //渲染图书列表
    async initList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      //  console.log(res.data);
      this.cateList = res.data
    }
  }
}

</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
